home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / processes / mpdelayuntiltest / mpdelayuntiltest.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.9 KB  |  176 lines

  1. /*
  2.     File:        "MPDelayUntilTest.c"
  3.     
  4.     Description:    This is a application to test/demo the MT/MP MPDelayUntil API.
  5.  
  6.     Version:    v0.0
  7.  
  8.     File Ownership:
  9.  
  10.         DRI:                George Warner
  11.  
  12.         Other Contact:        
  13.  
  14.         Technology:            MultiTasking/MultiProcessing
  15.  
  16.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  17.     
  18.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  19.                 ("Apple") in consideration of your agreement to the following terms, and your
  20.                 use, installation, modification or redistribution of this Apple software
  21.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  22.                 please do not use, install, modify or redistribute this Apple software.
  23.  
  24.                 In consideration of your agreement to abide by the following terms, and subject
  25.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  26.                 copyrights in this original Apple software (the "Apple Software"), to use,
  27.                 reproduce, modify and redistribute the Apple Software, with or without
  28.                 modifications, in source and/or binary forms; provided that if you redistribute
  29.                 the Apple Software in its entirety and without modifications, you must retain
  30.                 this notice and the following text and disclaimers in all such redistributions of
  31.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  32.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  33.                 Apple Software without specific prior written permission from Apple.  Except as
  34.                 expressly stated in this notice, no other rights or licenses, express or implied,
  35.                 are granted by Apple herein, including but not limited to any patent rights that
  36.                 may be infringed by your derivative works or by other works in which the Apple
  37.                 Software may be incorporated.
  38.  
  39.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  40.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  41.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  43.                 COMBINATION WITH YOUR PRODUCTS.
  44.  
  45.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  46.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  47.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  49.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  50.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  51.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52.  
  53.     Writers:
  54.  
  55.         gaw    - George Warner
  56.  
  57.     Change History (most recent first):
  58.  
  59.          <1>    02/29/00    gaw     Initial build
  60.  
  61.     Author Initials:
  62.  
  63.     gaw - George Warner
  64.  
  65. */
  66.  
  67. #pragma mark #Includes
  68. #include <MacTypes.h>
  69. #include <Fonts.h>
  70. #include <Multiprocessing.h>
  71.  
  72. #include <stdio.h>            // for printf & fflush
  73. #include <SIOUX.h>            // for SIOUXSettings stuff
  74. #include <SIOUXGlobals.h>    // for SIOUXQuitting
  75.  
  76. #pragma mark typedefs, structs, enums, defines, etc.
  77. typedef struct MyTaskParam_Struct
  78. {
  79.     Duration    fDuration;
  80.     OSStatus    fOSStatus;
  81.     Boolean*    fFlagPtr;
  82. } MyTaskParam_Rec,*MyTaskParam_Ptr,**MyTaskParam_Hdl;
  83.  
  84. #pragma mark static (local) functions
  85. // This is our MP task
  86. static OSStatus MyTask(void *parameter)
  87. {
  88.     // convert our (void*) parameter into a pointer to our task parameter record
  89.     MyTaskParam_Ptr tMyTaskParam_Ptr = (MyTaskParam_Ptr) parameter;
  90.     AbsoluteTime expirationTime;
  91.  
  92.     // calculate the expiration time based on the current
  93.     // absolute time plus a duration and arm the timer.
  94.     expirationTime = UpTime();
  95.     expirationTime = AddDurationToAbsolute(tMyTaskParam_Ptr->fDuration,expirationTime);
  96.  
  97.     // Ok, Delay until our expiration time.
  98.     tMyTaskParam_Ptr->fOSStatus = MPDelayUntil(&expirationTime);
  99.  
  100.     // set our done flag
  101.     *tMyTaskParam_Ptr->fFlagPtr = true;
  102.  
  103.     // and return our status.
  104.     return (tMyTaskParam_Ptr->fOSStatus);
  105. }
  106.  
  107. #pragma mark exported functions
  108. void main(void)
  109. {
  110.     MyTaskParam_Rec tMyTaskParam_Rec;
  111.     OSStatus        anErr;
  112.     MPTaskID        tMyMPTaskID;
  113.     UInt32            i, num;
  114.     volatile Boolean            done = false;
  115.     char            buff[10];    
  116.  
  117.     // Set the SIOUX window defaults
  118.     SIOUXSettings.autocloseonquit    = false;
  119.     SIOUXSettings.asktosaveonclose    = false;
  120.     SIOUXSettings.showstatusline    = false;
  121.     SIOUXSettings.columns            = 132;
  122.     SIOUXSettings.rows                = 24;
  123.     SIOUXSettings.fontsize            = 10;
  124.     GetFNum("\pMonaco",&SIOUXSettings.fontid);
  125.     SIOUXSettings.standalone        = true;
  126.  
  127.     printf("MPDelayUntilTest starting!\n");
  128.  
  129.     // Make sure that the MP library is loaded
  130.     if    (!MPLibraryIsLoaded())
  131.     {
  132.         printf("The MP library did not load.\n");
  133.         return;
  134.     }
  135.  
  136.     {    // for giggles we'll dump out the MP Library version info
  137.         Ptr tVersionCStringPtr;
  138.         UInt32 major,minor,release,revision;
  139.  
  140.         _MPLibraryVersion(&tVersionCStringPtr,&major,&minor,&release,&revision);
  141.  
  142.         printf("\n Version: \"%s\", major: %ld, minor: %ld, release: %ld, revision: %ld.\n",tVersionCStringPtr,major,minor,release,revision);
  143.     }
  144.  
  145.     // setup our task parameters
  146.     tMyTaskParam_Rec.fDuration = 10 * durationSecond;
  147.     tMyTaskParam_Rec.fOSStatus = noErr;
  148.     tMyTaskParam_Rec.fFlagPtr = (Boolean*) &done;
  149.  
  150.     // create our task
  151.     anErr = MPCreateTask(MyTask, (void *) &tMyTaskParam_Rec,0,NULL,0,0,kNilOptions,&tMyMPTaskID);
  152.     if (noErr != anErr)
  153.         printf(" MPCreateTask: %d.\n", anErr);
  154.     else
  155.         printf("Waiting 10 seconds...\n");
  156.  
  157.     // it's importaint that our WaitNextEvent loop not be CPU bound
  158.     // so we use a sleep time of one with WNE.
  159.     while (!done && (noErr == anErr) && !SIOUXQuitting)
  160.     {
  161.         EventRecord theEvent;
  162.  
  163.         WaitNextEvent(everyEvent,&theEvent,1,nil);
  164.         SIOUXHandleOneEvent(&theEvent);    // tell SIOUX to handle the event
  165.     }
  166.  
  167.     // if our task had an error then report it
  168.     if (noErr != tMyTaskParam_Rec.fOSStatus)
  169.         printf("MyTask error: %d.",tMyTaskParam_Rec.fOSStatus);
  170.  
  171.     if (noErr == anErr)
  172.         printf("Done!\n");
  173.  
  174.     printf("Press command-Q to quit.");
  175. }
  176.